<?php
namespace Admin\Controller;
use Component\AdminController;
//这个index不是入口文件，是admin的index
class IndexController extends AdminController
{
    //框架结构
    function index(){
        $this->display();
    }

    //头部页面
    function head()
    {
        $this->display();
    }

    //左边页面
    function left(){
        //根据session用户id信息查询角色id信息
        $sql = "select * from sw_manager where mg_id=".$_SESSION['mg_id'];
        $minfo = D()->query($sql);
        $role_id = $minfo[0]['mg_role_id'];
        //根据角色信息获得权限ids的信息
        $sql = "select * from sw_role where role_id=".$role_id;
        $rinfo = D()->query($sql);
        $auth_ids = $rinfo[0]['role_auth_ids'];
        //根据$auth_ids查询全部拥有的权限信息
        //① 获得顶级权限
        $sql = "select * from sw_auth where auth_level=0 and auth_id in ($auth_ids)";
//如果是admin管理员要现实全部权限
        $pauth_info = D()->query($sql);
        $sql = "select * from sw_auth where auth_level=1 and auth_id in ($auth_ids)";
//如果是admin管理员要现实全部权限
        $s_info = D()->query($sql);
        //var_dump($p_info);
        $this -> assign('pauth_info',$pauth_info);
        $this -> assign('sauth_info',$s_info);
        $this -> display();
    }

    //右边页面
    function right()
    {
        $this->assign("time",date('y-m-d h:i:s',time()));
        $this->display();
    }

    //管理用户
    function man_user(){
        $user = D("User");
        $total = $user->count();
        $per = 10;
        $page = new \Component\Page($total, $per);
        $sql = "select * from sw_user ";
        $sql=$sql. $page->limit;
        $minfo = $user->query($sql);
        $pagelist = $page->fpage();
        $this -> assign('minfo',$minfo);
        $this->assign('pagelist', $pagelist);//跳转导航列表
        $this -> display();
    }

    //删除用户
    function del_user($user_id){
        $user = D("User");
        $rst = $user->delete($user_id);
        if ($rst > 0) {
            $this -> redirect('man_user');
        } else {
            $this -> redirect('man_user','违规用户太顽强，难以根除！');
        }
    }

    //管理分享
    function man_message(){
        $message=D("Message");
        $total = $message->count();
        $per = 10;
        $page = new \Component\Page($total, $per);
        $sql = "select * from sw_message order by message_time desc ";
        $sql=$sql. $page->limit;
        $minfo = $message->query($sql);
        $pagelist = $page->fpage();
        $this->assign('minfo', $minfo);
        $this->assign('pagelist', $pagelist);//跳转导航列表
        $this -> display();
    }

    //删除分享条目
    function del_message($message_id){
        $message = D("Message");
        $rst = $message->delete($message_id);
        if ($rst > 0) {
            $this -> redirect('man_message');
        } else {
            $this -> redirect('man_message','违规信息太顽强，无法删除，我们都要因此毁灭了！');
        }
    }

    //管理员工
    function man_manager(){
        $man=D("Manager");
        $total = $man->count();
        $per = 10;
        $page = new \Component\Page($total, $per);
        $sql = "select * from sw_manager where mg_id>1 ";
        $sql=$sql. $page->limit;
        $minfo = $man->query($sql);
        $pagelist = $page->fpage();
        $this->assign('pagelist', $pagelist);//跳转导航列表

        $role = D(Role);
        foreach($minfo as $k => $v){
            $rinfo=$role->find($v["mg_role_id"]);
            $minfo[$k]["role_name"]=$rinfo["role_name"];
        }
        $this -> assign('minfo',$minfo);
        $this -> display();

    }

    //删除员工
    function del_manager($mg_id){
        $user = D("Manager");
        $rst = $user->delete($mg_id);
        if ($rst > 0) {
            $this -> redirect('man_manager');
        } else {
            $this -> redirect('man_manager','员工删除失败！');
        }
    }

    //添加员工
    function add_manager()
    {
        $role=D("Role");
        $rinfo=$role->where("role_id > 0")->select();
        $this->assign("rinfo",$rinfo);
        $man = D("Manager");
        if (!empty($_POST)) {
            if(trim($_POST['mg_name'])!=""&&trim($_POST['mg_pwd'])!=""){
                $man->create(); //收集post表单数据
                $z = $man->add();
                if ($z) {
                    $this->redirect("man_manager");
                } else {
                    $this->assign("error","添加失败，此员工是否存在？");
                }
            }else{
                $this->assign("error","添加失败，存在输入为空！");
            }
        }
        $this->display();
    }
    function upd_manager($mg_id)
    {
        $man= D("Manager");
        $minfo = $man->find($mg_id);
        $this->assign("minfo",$minfo);

        $role=D("Role");
        $rinfo=$role->where("role_id > 0")->select();
        $this->assign("rinfo",$rinfo);

        if (!empty($_POST)) {
            if(trim($_POST['mg_name'])!="") {
                $man->create(); //收集post表单数据
                $z = $man->save();
                if ($z) {
                    $this->redirect("man_manager");
                } else {
                    $this->redirect("man_manager");
                }
            }else{
                $this->assign("error","修改失败，存在输入为空！");
            }
        }
        $this -> display();
    }

    //修改密码
    public function chpwd(){
        $man = new \Model\ManagerModel();
        //两个逻辑：展示表单、收集表单
        if (!empty($_POST)) {
            $rst = $man -> checkNamePwd($_SESSION['mg_username'],$_POST['password3']);
            if($rst === false){
                $this->assign("pwd_error","旧密码错误");
            } else {
                if(trim($_POST['mg_pwd'])!="") {
                    $man->create();
                    $rst = $man->save();
                    if ($rst) {
                        $this->assign("pwd_error", "修改成功");
                    } else {
                        $this->assign("pwd_error", "新旧密码一致，无修改");
                    }
                }else{
                    $this->assign("pwd_error", "新密码为空，修改失败");
                }
            }
        }
        $this->display();
    }

    //管理评论
    function comment($message_id){
        $message=D("Message");
        $minfo=$message->find($message_id);
        $this->assign("minfo",$minfo);

        $comment=D("Comment");
        $total = $comment->where("message_id=".$message_id)->count();
        $per = 10;
        $page = new \Component\Page($total, $per);
        $sql = "SELECT * FROM `sw_comment` WHERE ( message_id=".$message_id.") order by comment_time desc ";
        $sql=$sql.$page->limit;
        $cinfo = $message->query($sql);
        $pagelist = $page->fpage();
        $this->assign('cinfo', $cinfo);
        $this->assign('pagelist', $pagelist);//跳转导航列表
        $this->assign('message_id', $message_id);
        $this->display();
    }

    //删除评论
    function del_comment($comment_id,$message_id){
        $comment=D("Comment");
        $rst=$comment->delete($comment_id);
        if ($rst) {
            $this->redirect("/admin/index/comment/message_id/$message_id");
        }else {
            $this->redirect("/admin/index/comment/message_id/$message_id",'删除失败。');
        }
    }

}